// 弹框样式
function dialog(msg, url = false, type = 0, fn = false, isJump = true){
    layui.use('layer', function(){
        var layer = layui.layer;

        if(type == 0){
            layer.msg(msg, {
                time: 1500,
                success: function (){
                    // 按下回车阻止再次提交
                    $(document).on('keydown keyup', function (e){
                        var Event = e || event;

                        if(Event.keyCode == 13){
                            return false;
                        }
                    })
                }
            }, function (){
                $(document).unbind('keydown keyup');
                if(fn){
                    fn();
                }

                if(url && isJump){
                    window.location = url;
                }
            });
        }else{
            layer.msg(msg, {
                time: 0,
                btn: ['确认', '取消'],
                yes: function(index){
                    layer.close(index);
                    layer.msg('删除成功', {
                        time: 1500,
                    });
                },
                success: function (){
                    // 按下回车阻止再次提交
                    $(document).on('keydown keyup', function (e){
                        var Event = e || event;

                        if(Event.keyCode == 13){
                            return false;
                        }
                    })
                }
            });
        }
    });
}

// 表单提交
function formData(url, name, fn = false, isJump = true){

    layui.use('form', function(){
        var form = layui.form;
        form.on('submit('+ name +')', function(data){
            var token = $("input[name='__token__']",top.window.document).val();
            data.field.__token__ = token;

            $.post(url, data.field, function(res){
                if(res.code == 0){
                    dialog(res.msg, false, 0, function (){
                        $("input[name='__token__']",top.window.document).val(res.data.token);
                    });
                }else if(res.code == 1){
                    dialog(res.msg, res.url, 0, function (){
                        $("input[name='__token__']",top.window.document).val(res.data.token);
                        if(fn)fn();
                    }, isJump);
                }
            }, 'json');

            return false;
        });
    });
}
